package com.ruoyi.park.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.park.domain.BuiGreenArea;
import com.ruoyi.park.domain.vo.BuiRoomVO;
import com.ruoyi.park.domain.vo.GreenInfoExcelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.park.domain.WyGreenInfo;
import com.ruoyi.park.service.IWyGreenInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 绿化信息Controller
 *
 * @author cjie
 * @date 2021-07-22
 */
@Api(tags = " 绿化信息")
@RestController
@RequestMapping("/wyGreenInfo")
public class WyGreenInfoController extends BaseController {
    @Autowired
    private IWyGreenInfoService wyGreenInfoService;

    /**
     * 查询绿化信息列表
     */
    @ApiOperation(value="查询绿化信息列表", notes="查询绿化信息列表")
    @PreAuthorize(hasPermi = "park:wyGreenInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(WyGreenInfo wyGreenInfo) {
        startPage();
        List<WyGreenInfo> list = wyGreenInfoService.selectWyGreenInfoList(wyGreenInfo);
        return getDataTable(list);
    }

    /**
     * 导出绿化信息列表
     */
    @ApiOperation(value="导出绿化信息列表", notes="导出绿化信息列表")
    @PreAuthorize(hasPermi = "park:wyGreenInfo:export")
    @Log(title = "绿化信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyGreenInfo wyGreenInfo) throws IOException {
        List<WyGreenInfo> list = wyGreenInfoService.selectWyGreenInfoList(wyGreenInfo);
        ExcelUtil<WyGreenInfo> util = new ExcelUtil<WyGreenInfo>(WyGreenInfo.class);
        util.exportExcel(response, list, "绿化信息数据");
    }
    /**
     * 获取绿化信息详细信息
     */
    @ApiOperation(value="获取绿化信息详细信息", notes="获取绿化信息详细信息")
    @PreAuthorize(hasPermi = "park:wyGreenInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(wyGreenInfoService.selectWyGreenInfoById(id));
    }

    /**
     * 新增绿化信息
     */
    @ApiOperation(value="新增绿化信息", notes="新增绿化信息")
    @PreAuthorize(hasPermi = "park:wyGreenInfo:add")
    @Log(title = "绿化信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<WyGreenInfo>  wyGreenInfos) {
        for (WyGreenInfo wyGreenInfo :wyGreenInfos){
            wyGreenInfoService.insertWyGreenInfo(wyGreenInfo);
        }
        return AjaxResult.success("新增成功");
    }

    /**
     * 修改绿化信息
     */
    @ApiOperation(value="修改绿化信息", notes="修改绿化信息")
    @PreAuthorize(hasPermi = "park:wyGreenInfo:edit")
    @Log(title = "绿化信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyGreenInfo wyGreenInfo) {
        return toAjax(wyGreenInfoService.updateWyGreenInfo(wyGreenInfo));
    }

    /**
     * 删除绿化信息
     */
    @ApiOperation(value="删除绿化信息", notes="删除绿化信息")
    @ApiImplicitParam(name = "ids", value = "绿化信息ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "park:wyGreenInfo:remove")
    @Log(title = "绿化信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(wyGreenInfoService.deleteWyGreenInfoByIds(ids));
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<GreenInfoExcelVO> util = new ExcelUtil<GreenInfoExcelVO>(GreenInfoExcelVO.class);
        util.importTemplateExcel(response, "绿化信息数据");
    }

    @ApiOperation(value = "导入绿化数据", notes = "导入绿化数据")
    @PreAuthorize(hasPermi = "park:wyGreenInfo:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<GreenInfoExcelVO> util = new ExcelUtil<GreenInfoExcelVO>(GreenInfoExcelVO.class);
        List<GreenInfoExcelVO> greenInfoExcelVOS = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = wyGreenInfoService.importData(greenInfoExcelVOS);


        return AjaxResult.success(message);

    }





}
